package com.leyou.goods.service;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Map;


@Service
public class GoodsHtmlService {
    @Autowired
    private TemplateEngine engine;
    @Autowired
    private GoodsService goodsService;

    private static final Logger LOGGER = LoggerFactory.getLogger(GoodsHtmlService.class);
    /**
    * @Description: 创建html页面
    * @Param:
    * @return:
    * @Author: XinWei Wu
    * @Date: 2020/6/11
    */
    public void createHtml(Long spuId){
        PrintWriter writer = null;
        //获取页面数据
        Map<String, Object> spuMap = this.goodsService.loadData(spuId);
        //创建thymeleaf上下文对象
        Context context = new Context();
        context.setVariables(spuMap);
        PrintWriter printWriter = null;
        try {
            File file = new File("F:\\JavaEE\\tool\\nginx-1.14.0\\html\\item" + spuId + ".html");
            printWriter = new PrintWriter(file);
            this.engine.process("item",context,printWriter);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (printWriter != null){
                printWriter.close();
            }
        }
    }


}
